package com.ihd.ihardware.view.tzc.me.view;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
import com.ihd.ihardware.R;
import com.ihd.ihardware.common.AppConstans;
import com.ihd.ihardware.databinding.ActivityMypageBinding;
import com.ihd.ihardware.pojo.OtherUserInfoRes;
import com.ihd.ihardware.pojo.SignsRes;
import com.ihd.ihardware.pojo.UserInfoRes;
import com.ihd.ihardware.view.tzc.me.viewmodel.MyPageViewModel;

import cn.wowjoy.commonlibrary.base.BaseActivity;
import cn.wowjoy.commonlibrary.base.BaseConsumer;
import cn.wowjoy.commonlibrary.rxbus.RxBus;
import cn.wowjoy.commonlibrary.utils.DialogUtils;
import cn.wowjoy.commonlibrary.utils.SPUtils;
import cn.wowjoy.commonlibrary.utils.ToastUtils;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class MyPageActivity extends BaseActivity<ActivityMypageBinding, MyPageViewModel> {
    private UserInfoRes.DataBean ud;
    private String userId;
    private OtherUserInfoRes ouinfo;
    private Disposable msg;

    @Override
    protected void init(Bundle savedInstanceState) {
        mImmersionBar.statusBarDarkFont(false).init();

        userId = getIntent().getStringExtra("userId");
        ud = new Gson().fromJson(SPUtils.getString(AppConstans.USER_INFO, ""), UserInfoRes.DataBean.class);

        if (!TextUtils.isEmpty(userId)) {
            viewModel.getOtherUserInfo(userId);
            binding.mConcernTV.setText("他的关注");
            binding.mFansTV.setText("他的粉丝");
            binding.concernLL.setVisibility(View.VISIBLE);
        } else {
            if (TextUtils.isEmpty(ud.getAvatar())) {
                if (ud.getSex() == 1)
                    binding.headIV.setImageResource(R.drawable.head_man);
                else
                    binding.headIV.setImageResource(R.drawable.head_women);
            } else {
                Glide.with(this)
                        .load(Uri.parse(ud.getAvatar()))
                        .apply(RequestOptions.circleCropTransform())
                        .into(binding.headIV);
                Glide.with(this)
                        .load(Uri.parse(ud.getAvatar()))
                        .into(binding.myPageBG);
            }
            binding.fsTag.setText(ud.getFansNum());
            binding.gzTag.setText(ud.getConcernNum());
            binding.nicknameTV.setText(ud.getNickName());
        }
        viewModel.getSigns(userId, 1, 100);

        binding.mtitlebar.setLeftBack(this);
        binding.mtitlebar.setLeftImageResource(R.drawable.ic_back_w);
//        binding.mtitlebar.addAction(new TitleBar.Action() {
//            @Override
//            public String getText() {
//                return null;
//            }
//
//            @Override
//            public int getDrawable() {
//                return R.drawable.ic_more;
//            }
//
//            @Override
//            public void performAction(View view) {
//
//            }
//        });
        binding.dtRV.setNestedScrollingEnabled(false);
        binding.dtRV.setAdapter(viewModel.mCommonAdapter);

        binding.mConcernLL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(userId)) {
                    Intent i = new Intent(MyPageActivity.this, ConcernActivity.class);
                    startActivity(i);
                }
            }
        });

        binding.mFansLL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(userId)) {
                    Intent i = new Intent(MyPageActivity.this, FansActivity.class);
                    startActivity(i);
                }
            }
        });

        binding.concernLL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewModel.setConcern(ouinfo.getData());
            }
        });

        setRx(AppConstans.PAGE, new BaseConsumer<SignsRes>() {
            @Override
            protected void onStart() {
                DialogUtils.waitingDialog(MyPageActivity.this);
            }

            @Override
            protected void onSuccess(SignsRes mLiveData) {
                DialogUtils.dismiss(MyPageActivity.this);
                if (mLiveData.getData().getSignDetails().size() == 0)
                    binding.emptyLL.setVisibility(View.VISIBLE);
                else
                    binding.emptyLL.setVisibility(View.GONE);
                viewModel.setSigns(mLiveData.getData().getSignDetails());
            }

            @Override
            protected void onError(Throwable error) {
                ToastUtils.showShort(MyPageActivity.this, error.getMessage());
                DialogUtils.dismiss(MyPageActivity.this);
            }
        });

        setRx(AppConstans.INFOS, new BaseConsumer<OtherUserInfoRes>() {
            @Override
            protected void onStart() {
            }

            @Override
            protected void onSuccess(OtherUserInfoRes mLiveData) {
                ouinfo = mLiveData;
                if (TextUtils.isEmpty(mLiveData.getData().getAvatar())) {
                    if (mLiveData.getData().getSex().equals(1))
                        binding.headIV.setImageResource(R.drawable.head_man);
                    else
                        binding.headIV.setImageResource(R.drawable.head_women);
                } else {
                    Glide.with(MyPageActivity.this)
                            .load(Uri.parse(mLiveData.getData().getAvatar()))
                            .apply(RequestOptions.circleCropTransform())
                            .into(binding.headIV);
                    Glide.with(MyPageActivity.this)
                            .load(Uri.parse(mLiveData.getData().getAvatar()))
                            .into(binding.myPageBG);
                }
                binding.fsTag.setText(mLiveData.getData().getFansNum());
                binding.gzTag.setText(mLiveData.getData().getConcernNum());
                binding.nicknameTV.setText(mLiveData.getData().getNickName());
                if (mLiveData.getData().isConcern()) {
                    binding.concernLL.setBackgroundResource(R.drawable.corners_5661f3);
                    binding.concernIV.setVisibility(View.GONE);
                    binding.concernTV.setText("已关注");
                    binding.concernTV.setTextColor(binding.concernTV.getContext().getResources().getColor(R.color.C_FFFFFF));
                } else {
                    binding.concernLL.setBackgroundResource(R.drawable.corners_line_bg);
                    binding.concernIV.setVisibility(View.VISIBLE);
                    binding.concernTV.setText("关注");
                    binding.concernTV.setTextColor(binding.concernTV.getContext().getResources().getColor(R.color.C_5661F3));
                }
            }

            @Override
            protected void onError(Throwable error) {
                ToastUtils.showShort(MyPageActivity.this, error.getMessage());
            }
        });

        msg = RxBus.getInstance().tObservable(AppConstans.CONCERN_PAGE, Bundle.class)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Bundle>() {
                    @Override
                    public void accept(Bundle b) throws Exception {
                        if (b.getInt("type") == 1) {
                            ouinfo.getData().setConcern(true);
                            binding.fsTag.setText(String.valueOf((Integer.parseInt(binding.fsTag.getText().toString()) + 1)));
                            binding.concernLL.setBackgroundResource(R.drawable.corners_5661f3);
                            binding.concernIV.setVisibility(View.GONE);
                            binding.concernTV.setText("已关注");
                            binding.concernTV.setTextColor(binding.concernTV.getContext().getResources().getColor(R.color.C_FFFFFF));
                        } else {
                            binding.fsTag.setText(String.valueOf((Integer.parseInt(binding.fsTag.getText().toString()) - 1)));
                            ouinfo.getData().setConcern(false);
                            binding.concernLL.setBackgroundResource(R.drawable.corners_line_bg);
                            binding.concernIV.setVisibility(View.VISIBLE);
                            binding.concernTV.setText("关注");
                            binding.concernTV.setTextColor(binding.concernTV.getContext().getResources().getColor(R.color.C_5661F3));
                        }
                    }
                });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_mypage;
    }

    @Override
    protected Class getViewModelClass() {
        return MyPageViewModel.class;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (msg != null && !msg.isDisposed())
            msg.dispose();
    }
}
